package com.example.myapplication.utils;

import com.example.myapplication.R;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class HttpUtils {

    public static void Get(String action, Callback callback) {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(UrlFactory(action))
                .get()
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(callback);
    }

    public static void Post(String action, String json, Callback callback) {
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody body = FormBody.create(MediaType.parse("application/json;charset=utf-8"), json);
        Request request = new Request.Builder()
                .url(action)
                .post(body)
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(callback);
    }

    public static String UrlFactory(String action){
        if (action.contains("http")){
            return action;
        }else {
            return Config.URL + action;
        }
    }
}
